#include "process.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

//函数指针类型
//void (*) (double, double);
typedef void (*call_t) (double, double);


double total = 1024.0;
//double speed = 1.0;
double speed[] = {5.0, 0.3, 0.2, 0.1, 0.01};
int len = sizeof(speed)/sizeof(speed[0]);

void download(double total, call_t cb)
{
    srand(time(NULL));
    double current = 0.0;
    while(current <= total)
    {
        cb(total, current);//进行回调
        if(current >= total) break;
        //下载逻辑代码（现阶段用usleep代替）
        int random = rand()%len;
        usleep(5000);
        current += speed[random];
        if(current > total)
        {
            current = total;
        }
    }
    printf("\n");
}

int main()
{
    download(1024.0, FlushProcess);
    printf("download 1024.0MB done\n");
    download(512.0, FlushProcess);
    printf("download 512.0MB done\n");
    download(256.0, FlushProcess);
    printf("download 256.0MB done\n");
    download(128.0, FlushProcess);
    printf("download 128.0MB done\n");
    return 0;
}




